package svc

import (
	"database/sql"

	"mall/common/modelx"
	"mall/service/user/repository"
	"mall/service/user/rpc/internal/config"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/redis"
)

type ServiceContext struct {
	Config config.Config

	BizRedis *redis.Redis

	DB          *sql.DB
	CurrentUser repository.CurrentUserRepository
	UserAddress repository.UserAddressRepository
	UserFollow  repository.UserFollowRepository

	User repository.UserRepository
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := modelx.NewMysqlConn(c.Mysql.DataSource)
	if err != nil {
		logx.Must(err)
	}

	rds := redis.MustNewRedis(c.BizRedis)

	return &ServiceContext{
		Config: c,

		BizRedis: rds,

		DB:          db,
		CurrentUser: repository.NewCurrentUserRepository(db, c.CacheRedis),
		UserAddress: repository.NewUserAddressRepository(db, c.CacheRedis),
		UserFollow:  repository.NewUserFollowRepository(db, c.CacheRedis),

		User: repository.NewUserRepository(c.Mysql.DataSource, c.CacheRedis),
	}
}
